//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "OneFactorCopula.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/Distribution.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::COneFactorCopula::COneFactorCopula (boost::shared_ptr<QuantLib::OneFactorCopula>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOneFactorCopula = NULL;
#endif
	_ppOneFactorCopula = &childNative;
    
}
Cephei::QL::Experimental::Credit::COneFactorCopula::COneFactorCopula (QuantLib::OneFactorCopula& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phOneFactorCopula = NULL;
#endif
	_ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (&childNative);
    
    _OneFactorCopulaOwner = owner;
    
}

Cephei::QL::Experimental::Credit::COneFactorCopula::COneFactorCopula (COneFactorCopula^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOneFactorCopula = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::COneFactorCopula::COneFactorCopula (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOneFactorCopula = NULL;
#endif
	if (!t->IsSubclassOf(COneFactorCopula::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::COneFactorCopula::COneFactorCopula (QuantLib::Handle<QuantLib::OneFactorCopula>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOneFactorCopula = &childNative;
	_ppOneFactorCopula = &static_cast<boost::shared_ptr<QuantLib::OneFactorCopula>>(childNative.currentLink());
    
    _OneFactorCopulaOwner = owner;
}
Cephei::QL::Experimental::Credit::COneFactorCopula::COneFactorCopula (QuantLib::Handle<QuantLib::OneFactorCopula> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phOneFactorCopula = &childNative;
	_ppOneFactorCopula = &static_cast<boost::shared_ptr<QuantLib::OneFactorCopula>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::COneFactorCopula::COneFactorCopula (QuantLib::OneFactorCopula childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phOneFactorCopula = NULL;
#endif
	_ppOneFactorCopula = new boost::shared_ptr<QuantLib::OneFactorCopula> (new QuantLib::OneFactorCopula (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::COneFactorCopula::~COneFactorCopula ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppOneFactorCopula != NULL)
    {
	    delete _ppOneFactorCopula;
        _ppOneFactorCopula = NULL;
    }
}
Cephei::QL::Experimental::Credit::COneFactorCopula::!COneFactorCopula ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppOneFactorCopula != NULL)
    {
	    delete _ppOneFactorCopula;
    }
}
QuantLib::OneFactorCopula& Cephei::QL::Experimental::Credit::COneFactorCopula::GetReference ()
{
    if (_ppOneFactorCopula == NULL) throw gcnew NativeNullException ();
	return **_ppOneFactorCopula;
}
boost::shared_ptr<QuantLib::OneFactorCopula>& Cephei::QL::Experimental::Credit::COneFactorCopula::GetShared ()
{
    if (_ppOneFactorCopula == NULL) throw gcnew NativeNullException ();
	return *_ppOneFactorCopula;
}
QuantLib::OneFactorCopula* Cephei::QL::Experimental::Credit::COneFactorCopula::GetPointer ()
{
    if (_ppOneFactorCopula == NULL) throw gcnew NativeNullException ();
	return &**_ppOneFactorCopula;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::OneFactorCopula>& Cephei::QL::Experimental::Credit::COneFactorCopula::GetHandle ()
{
	if (_phOneFactorCopula == NULL)
	{
		_phOneFactorCopula = new Handle<QuantLib::OneFactorCopula> (*_ppOneFactorCopula);
	}
	return *_phOneFactorCopula;
}
#endif
bool Cephei::QL::Experimental::Credit::COneFactorCopula::HasNative () 
{
	return (_ppOneFactorCopula != NULL);
}

Int32 Cephei::QL::Experimental::Credit::COneFactorCopula::CheckMoments (Double tolerance)
{
    try
    {
        QuantLib::Real _tolerance = (QuantLib::Real)ValueHelper::Convert (tolerance);
    	int _rv = (int)(*_ppOneFactorCopula)->checkMoments ( _tolerance );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Experimental::Credit::COneFactorCopula::ConditionalProbability (Cephei::IVector<Double>^ prob, Double m)
{
    CoVector<Double>^ _Cprob;
    try
    {
        _Cprob = safe_cast<CoVector<Double>^> (prob);
        _Cprob->Lock();
        INativeVector<Double>^ _NCIprob = _Cprob->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCprob = safe_cast<CDoubleVector^>(_NCIprob);
        std::vector<QuantLib::Real>& _prob = static_cast<std::vector<QuantLib::Real>&> (_NCprob->GetReference ());
        QuantLib::Real _m = (QuantLib::Real)ValueHelper::Convert (m);
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppOneFactorCopula)->conditionalProbability ( _prob,  _m );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprob != nullptr) _Cprob->Unlock();
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorCopula::Correlation::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorCopula)->correlation ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorCopula::CumulativeY (Double y)
{
    try
    {
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorCopula)->cumulativeY ( _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorCopula::CumulativeZ (Double z)
{
    try
    {
        QuantLib::Real _z = (QuantLib::Real)ValueHelper::Convert (z);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorCopula)->cumulativeZ ( _z );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorCopula::Density (Double m)
{
    try
    {
        QuantLib::Real _m = (QuantLib::Real)ValueHelper::Convert (m);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorCopula)->density ( _m );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorCopula::Integral (Double p)
{
    try
    {
        QuantLib::Real _p = (QuantLib::Real)ValueHelper::Convert (p);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorCopula)->integral ( _p );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::COneFactorCopula::InverseCumulativeY (Double p)
{
    try
    {
        QuantLib::Real _p = (QuantLib::Real)ValueHelper::Convert (p);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppOneFactorCopula)->inverseCumulativeY ( _p );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

